<?php
/**
* @file $Id: functions.php 583 2007-06-05 19:38:25Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

// OTHER INFO
function _makeTextInput($column,$name,$size)
{	global $value,$field;

	if($_REQUEST['student_id']=='new' && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif($_REQUEST['student_id']=='new')
		$div = true;
	else
		$div = 'force';

	if($field['TYPE']=='numeric')
		$value[$column] = str_replace('.00','',$value[$column]);

	return TextInput($value[$column],'students['.$column.']',$name,$size,$div);
}

function _makeDateInput($column,$name)
{	global $value,$field;

	if($_REQUEST['student_id']=='new' && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif($_REQUEST['student_id']=='new')
		$div = true;
	else
		$div = 'force';

	if($value[$column] && strlen($value[$column])==9)
	{
		$year = substr($value[$column],7);
		$month = $months_number[strtoupper(substr($value[$column],3,3))];
		$day = substr($value[$column],0,2)*1;
	}
	elseif($value[$column])
	{
		$year = substr($value[$column],0,4);
		$month = substr($value[$column],5,2)*1;
		$day = substr($value[$column],8)*1;
	}

	if($value[$column])
	{
		$time = mktime(0,0,0,$month,$day,$year);
		$diff = time() - $time;
		$days = floor($diff / 86400);
		if($days > 1)
		{
			if($days < 31)
				$extra = $days.' '._('days ago');
			elseif($days < 365)
				$extra = round($days/30.417,1).' '._('months ago');
			else
				$extra = floor($days/365).' '._('years ago');
		}
	}

	return DateInput($value[$column],'students['.$column.']',$name,$div);
}

function _makeSelectInput($column,$name)
{	global $value,$field;

	if($_REQUEST['student_id']=='new' && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif($_REQUEST['student_id']=='new')
		$div = true;
	else
		$div = 'force';

	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$select_options = explode("\r",$field['SELECT_OPTIONS']);
	if(count($select_options))
	{
		foreach($select_options as $option)
			$options[$option] = $option;
	}

	$extra = 'style="max-width:250;"';
	return SelectInput($value[$column],'students['.$column.']',$name,$options,'N/A',$extra,$div);
}

function _makeCheckboxInput($column,$name)
{	global $value,$field;

	if($_REQUEST['student_id']=='new' && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	else
		$div = true;

	return CheckboxInput($value[$column],'students['.$column.']',$name,'',($_REQUEST['student_id']=='new'?true:false));
}

function _makeTextareaInput($column,$name)
{	global $value,$field;

	if($_REQUEST['student_id']=='new' && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif($_REQUEST['student_id']=='new')
		$div = true;
	else
		$div = 'force';

	return TextAreaInput($value[$column],'students['.$column.']',$name,'',$div);
}

function _makeMultipleInput($column,$name)
{	global $value,$field,$_FOCUS;

	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
		$select_options = explode("\r",$field['SELECT_OPTIONS']);
		if(count($select_options))
		{
			foreach($select_options as $option)
			{
				$options[$option] = $option;
			}
		}

		if($value[$column]!='')
			echo "<DIV id='divstudents[".$column."]'><div onclick='javascript:addHTML(\"<INPUT type=hidden name=students[".$column."] value=\\\"\\\">";
		echo '<TABLE border=0 cellpadding=3>';
		if(count($options)>12)
		{
			echo '<TR><TD colspan=2>';
			echo '<small><b>'.$name.'<b></small>';
			if($value[$column]!='')
				echo '<TABLE width=100% height=7 style=\"border:1;border-style: solid solid none solid;\"><TR><TD></TD></TR></TABLE>';
			else
				echo '<TABLE width=100% height=7 style="border:1;border-style: solid solid none solid;"><TR><TD></TD></TR></TABLE>';

			echo '</TD></TR>';
		}
		echo '<TR>';
		$i = 0;
		foreach($options as $option)
		{
			if($i%2==0)
				echo '</TR><TR>';
			if($value[$column]!='')
				echo '<TD><INPUT type=checkbox name=students['.$column.'][] value=\"'.str_replace("'",'"+single_quote+"',str_replace('"','"+double_quote+"',$option)).'\"'.(strpos($value[$column],'||'.$option.'||')!==false?' CHECKED':'').'><small>'.str_replace("'",'"+single_quote+"',str_replace('"','"+"\""+"',$option)).'</small></TD>';
			else
				echo '<TD><INPUT type=checkbox name=students['.$column.'][] value="'.$option.'"'.(strpos($value[$column],'||'.$option.'||')!==false?' CHECKED':'').'><small>'.$option.'</small></TD>';
			$i++;
		}
		echo '</TR><TR><TD colspan=2>';
		if($value[$column]!='')
			echo '<TABLE width=100% height=7 style=\"border:1;border-style: none solid solid solid;\"><TR><TD></TD></TR></TABLE>';
		else
			echo '<TABLE width=100% height=7 style="border:1;border-style: none solid solid solid;"><TR><TD></TD></TR></TABLE>';

		echo '</TD></TR></TABLE>';

		if($value[$column]!='')
			echo "\",\"divstudents[".$column."]"."\",true);' >".(($value[$column]!='')?str_replace('||',', ',substr($value[$column],2,-2)):'-')."</div></DIV>";
	}
	else
		echo (($value[$column]!='')?str_replace('||',', ',substr($value[$column],2,-2)):'-<BR>');

	echo '<small><b>'.$name.'<b></small>';
}

// MEDICAL ----
function _makeDate($value,$column='MEDICAL_DATE')
{	global $THIS_RET,$table;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	$div = true;
	if($column!='MEDICAL_DATE' && $THIS_RET['ID']=='new' && !$value)
	{
		$value = DBDate();
		$div = false;
	}

	return DateInput($value,'values['.$table.']['.$THIS_RET['ID'].']['.$column.']','',$div);
}

function _makeComments($value,$column)
{	global $THIS_RET,$table;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	return TextInput($value,'values['.$table.']['.$THIS_RET['ID'].']['.$column.']');
}

// LOG FIELDS
function _makeLogDate($value,$column)
{	global $THIS_RET,$table;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	$div = true;
	if($THIS_RET['ID']=='new' && !$value)
	{
		$value = DBDate();
		$div = false;
	}

	return DateInput($value,'log_values['.$table.']['.$THIS_RET['ID'].']['.$column.']','',$div);
}

function _makeLogText($value,$column)
{	global $THIS_RET,$table;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	if($column=='LOG_NUMERIC')
		$extra = 'size=4 maxlength=10';

	return TextInput($value,'log_values['.$table.']['.$THIS_RET['ID'].']['.$column.']','',$extra);
}

function _makeLogCheckbox($value,$column)
{	global $THIS_RET,$table;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	if($THIS_RET['ID']=='new')
		$new = true;
	else
		$new = false;

	return CheckboxInput($value,'log_values['.$table.']['.$THIS_RET['ID'].']['.$column.']','','',$new);
}

function _makeLogSelect($value,$column)
{	global $THIS_RET,$table,$log_options;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	$lo = str_replace("\n","\r",str_replace("\r\n","\r",$log_options));
	$lo = explode("\r",$lo);
	foreach($lo as $option)
		$options[$option] = $option;

	return SelectInput($value,'log_values['.$table.']['.$THIS_RET['ID'].']['.$column.']','',$options);
}

function _makeLogTextArea($value,$column)
{	global $THIS_RET,$table,$textarea_extra;

	if(!$THIS_RET['ID'])
		$THIS_RET['ID'] = 'new';

	return TextAreaInput($value,'log_values['.$table.']['.$THIS_RET['ID'].']['.$column.']','',$textarea_extra);
}

// ENROLLMENT
function _makeStartInput($value,$column)
{	global $THIS_RET,$add_codes;

	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	elseif($_REQUEST['student_id']=='new')
	{
		$id = 'new';
		$default = DBGet(DBQuery("SELECT min(SCHOOL_DATE) AS START_DATE FROM ATTENDANCE_CALENDAR WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
		$default = $default[1]['START_DATE'];
		if(!$default || DBDate('postgres')>$default)
			$default = DBDate();
		$value = $default;
	}
	else
	{
		$add = '<TD>'.button('add').'</TD>';
		$id = 'new';
	}

	if(!$add_codes)
	{
		$options_RET = DBGet(DBQuery("SELECT ID,TITLE AS TITLE FROM STUDENT_ENROLLMENT_CODES WHERE SYEAR='".UserSyear()."' AND TYPE='Add'"));

		if($options_RET)
		{
			foreach($options_RET as $option)
				$add_codes[$option['ID']] = $option['TITLE'];
		}
	}

	if($_REQUEST['student_id']=='new')
		$div = false;
	else
		$div = true;

	return '<TABLE class=innerLO_field><TR>'.$add.'<TD>'.DateInput($value,'values[STUDENT_ENROLLMENT]['.$id.']['.$column.']','',$div,true).'</TD><TD> - </TD><TD>'.SelectInput($THIS_RET['ENROLLMENT_CODE'],'values[STUDENT_ENROLLMENT]['.$id.'][ENROLLMENT_CODE]','',$add_codes).'</TD></TR></TABLE>';
}

function _makeEndInput($value,$column)
{	global $THIS_RET,$drop_codes;

	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';

	if(!$drop_codes)
	{
		$options_RET = DBGet(DBQuery("SELECT ID,TITLE AS TITLE FROM STUDENT_ENROLLMENT_CODES WHERE SYEAR='".UserSyear()."' AND TYPE='Drop'"));

		if($options_RET)
		{
			foreach($options_RET as $option)
				$drop_codes[$option['ID']] = $option['TITLE'];
		}
	}

	return '<TABLE class=innerLO_field><TR><TD>'.DateInput($value,'values[STUDENT_ENROLLMENT]['.$id.']['.$column.']').'</TD><TD> - </TD><TD>'.SelectInput($THIS_RET['DROP_CODE'],'values[STUDENT_ENROLLMENT]['.$id.'][DROP_CODE]','',$drop_codes).'</TD></TR></TABLE>';
}

function _makeSchoolInput($value,$column)
{	global $THIS_RET,$schools;

	// Since school cant be changed
	$id = 'new';

	if(!$schools)
		$schools = DBGet(DBQuery("SELECT ID,TITLE FROM SCHOOLS"));

	foreach($schools as $school)
		$options[$school['ID']] = $school['TITLE'];

	if($_REQUEST['student_id']!='new')
		return SelectInput(UserSchool(),'values[STUDENT_ENROLLMENT]['.$id.'][SCHOOL_ID]','',$options,'','',false);
	else
		return GetSchool(UserSchool());
}
?>